using System;
using System.Linq;

namespace DayDayUp.Core.Domain.Users
{
    public static class UserExtensions
    {

        /// <summary>
        /// Gets a value indicating whether customer is in a certain customer role
        /// </summary>
        /// <param name="user">Customer</param>
        /// <param name="customerRoleSystemName">Customer role system name</param>
        /// <param name="onlyActiveCustomerRoles">A value indicating whether we should look only in active customer roles</param>
        /// <returns>Result</returns>
        public static bool IsInCustomerRole(this User user,
            string customerRoleSystemName, bool onlyActiveCustomerRoles = true)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            if (String.IsNullOrEmpty(customerRoleSystemName))
                throw new ArgumentNullException("customerRoleSystemName");

            var result = user.UserRoles
                .Where(cr => !onlyActiveCustomerRoles || cr.Active)
                .Where(cr => cr.SystemName == customerRoleSystemName)
                .FirstOrDefault() != null;
            return result;
        }

       
        /// <summary>
        /// Gets a value indicating whether customer is administrator
        /// </summary>
        /// <param name="user">Customer</param>
        /// <param name="onlyActiveCustomerRoles">A value indicating whether we should look only in active customer roles</param>
        /// <returns>Result</returns>
        public static bool IsAdmin(this User user, bool onlyActiveCustomerRoles = true)
        {
            return IsInCustomerRole(user, SystemUserRoleNames.Administrators, onlyActiveCustomerRoles);
        }

       

        

        
        /// <summary>
        /// Gets a value indicating whether customer is registered
        /// </summary>
        /// <param name="customer">Customer</param>
        /// <param name="onlyActiveCustomerRoles">A value indicating whether we should look only in active customer roles</param>
        /// <returns>Result</returns>
        public static bool IsRegistered(this User user, bool onlyActiveCustomerRoles = true)
        {
            return IsInCustomerRole(user, SystemUserRoleNames.Registered, onlyActiveCustomerRoles);
        }

       
        /// <summary>
        /// Gets a value indicating whether customer is guest
        /// </summary>
        /// <param name="customer">Customer</param>
        /// <param name="onlyActiveCustomerRoles">A value indicating whether we should look only in active customer roles</param>
        /// <returns>Result</returns>
        public static bool IsGuest(this User user, bool onlyActiveCustomerRoles = true)
        {
            return IsInCustomerRole(user, SystemUserRoleNames.Guests, onlyActiveCustomerRoles);
        }
        
       
    }
}
